package cn.com.callback

import cn.com.elite.EliteBean
import cn.com.elite.Util
import cn.com.kh.KHDBConfig
import cn.com.kh.KHUtil
import cn.com.kh.LogToDB
import net.sf.json.JSONArray
import net.sf.json.JSONObject
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory
import org.springframework.jdbc.core.JdbcTemplate
import net.sf.json.JSONNull


/**
* @Title:获取客景系统事件回访数据API
* @Description:每天6:30跑一次
* @Param:
* @return:
* @Author: TaoHao
* @Date: 2024/6/20
*/
public class autoGetKJEventList implements  CallBackInvoke  {
    private static final Log log = LogFactory.getLog(autoGetKJEventList.class);


    //初始化请求入参返回参数
    JSONObject outData = new JSONObject();
    JSONObject inData = new JSONObject();
    long startTime = System.currentTimeMillis();
    @Override
    void doCallBack(EliteBean eliteBean, String s, Map map, String s1) {

        try {
     JdbcTemplate jt = new JdbcTemplate(eliteBean.getDataSource());
        //http://10.200.35.76/groovy/call/apiGetEventList
        //获取客景接口请求地址
        String KJ_URL = KHDBConfig.getAPIURL(eliteBean, "KJ_URL");

        Map<String, String> headMap = new HashMap<String, String>();
        headMap.put("Content-Type", "application/json");
        String PostBackBody=Util.doHttpPostUseHead(KJ_URL+"apiGetEventList","","UTF-8",50000000,0,headMap);

        JSONObject jsonObject =JSONObject.fromObject(PostBackBody);

        log.debug("【apiGetKJEventList】接口返回参数："+jsonObject.toString());

        //接口返回0成功  其他则失败
        if (KHUtil.GetSFJV(jsonObject,"code").equals("0")||KHUtil.GetSFJV(jsonObject,"code")==0){

            if (!JSONNull.getInstance().equals(jsonObject.get("data"))) {
                JSONArray dataArr = jsonObject.getJSONArray("data");

                for (int i= 0; i < dataArr.size(); i++) {
                    JSONObject dataJsonTwo = dataArr.getJSONObject(i);
                    String evnet_code = KHUtil.GetSFJV(dataJsonTwo, "evnet_code"); //事件编码
                    String event_name = KHUtil.GetSFJV(dataJsonTwo, "event_name"); //事件描述
                    String event_desc = KHUtil.GetSFJV(dataJsonTwo, "event_desc"); //事件描述
                    String bacth_guid = KHUtil.GetSFJV(dataJsonTwo, "bacth_guid"); //批次guid
                    String batch_id = KHUtil.GetSFJV(dataJsonTwo, "batch_id"); //批次ID
                    String num = KHUtil.GetSFJV(dataJsonTwo, "num"); //数量
                    String name = KHUtil.GetSFJV(dataJsonTwo, "name"); //名称
                    String EVENT_NOTICE = KHUtil.GetSFJV(dataJsonTwo, "event_notice"); //策略ID
                    String EVENT_NOTICE_NAME = KHUtil.GetSFJV(dataJsonTwo, "event_notice_name"); //策略名称
                    String batch_state = KHUtil.GetSFJV(dataJsonTwo, "batch_state"); //批次状态
                    String succeed_num = KHUtil.GetSFJV(dataJsonTwo, "succeed_num"); //成功数量
                    String lose_num = KHUtil.GetSFJV(dataJsonTwo, "batch_state"); //失败数量
                    String created_date=   KHUtil.GetSFJV(dataJsonTwo, "created_date")==''? null : KHUtil.GetSFJV(dataJsonTwo, "created_date");//创建事件
                    //先把原数据删除、再新增
                    String delBatchSql = '''DELETE FROM KJ_EVENT_BATCH WHERE BATCH_GUID=?''';
                    jt.update(delBatchSql, bacth_guid);

                    String insertSql = '''Insert Into KJ_EVENT_BATCH 
                                        (Batch_guid,
                                        event_code,
                                        event_name,
                                        event_desc,
                                        batch_id,
                                        num,
                                        name,
                                        event_notice,
                                        event_notice_name,
                                        batch_state,
                                        Sync_time,
                                        succeed_num,
                                        lose_num,
                                        created_date
                                        )values(
                                        ?,
                                        ?,
                                        ?,
                                        ?,
                                        ?,
                                        ?,
                                        ?,
                                        ?,
                                        ?,
                                        ?,
                                       Getdate(),
                                        ?,
                                        ?,
                                        ?
                                        )              
                                        ''';
                    jt.update(insertSql, bacth_guid, evnet_code, event_name, event_desc, batch_id, num, name, EVENT_NOTICE, EVENT_NOTICE_NAME, batch_state, succeed_num, lose_num,created_date);

                    if (!JSONNull.getInstance().equals(dataJsonTwo.get("BatchDescList"))) {
                        JSONArray BatchDescListArr = dataJsonTwo.getJSONArray("BatchDescList");
                        //先把原数据删除、再新增
                        String delBatchLISTSql = '''DELETE FROM KJ_EVENT_LIST WHERE BATCH_GUID=?''';
                        jt.update(delBatchLISTSql, bacth_guid);
                        for (int b = 0; b < BatchDescListArr.size(); b++) {
                            JSONObject BatchDescListjson = BatchDescListArr.getJSONObject(b);
                            String details_guid=KHUtil.GetSFJV(BatchDescListjson, "details_guid");//批次详情guid
                            String EVENT_CODE=KHUtil.GetSFJV(BatchDescListjson, "EVENT_CODE");//事件编号
                            String device_SERVICE=KHUtil.GetSFJV(BatchDescListjson, "device_SERVICE");//服务策略
                            String device_NOTICE=KHUtil.GetSFJV(BatchDescListjson, "device_NOTICE");//通知策略
                            String sen1NAME=KHUtil.GetSFJV(BatchDescListjson, "sen1NAME");//服务名称
                            String sen2NAME=KHUtil.GetSFJV(BatchDescListjson, "sen2NAME");//通知策略
                            String arrive_user=KHUtil.GetSFJV(BatchDescListjson, "arrive_user");//触达用户
                            String accept_number=KHUtil.GetSFJV(BatchDescListjson, "accept_number");//用户接收号码
                            String arrive_code=KHUtil.GetSFJV(BatchDescListjson, "arrive_code");//触达状态
                            String arrive_desc=KHUtil.GetSFJV(BatchDescListjson, "arrive_desc");//原因说明
                            String createddate=KHUtil.GetSFJV(BatchDescListjson, "created_date");//创建时间
                            String arrive_time=KHUtil.GetSFJV(BatchDescListjson, "arrive_time")==''? null : KHUtil.GetSFJV(BatchDescListjson, "arrive_time");//触达时间
                            String user_name=KHUtil.GetSFJV(BatchDescListjson, "user_name");//客户姓名
                            String InsertSql2='''
                                            Insert into KJ_EVENT_LIST(
                                            details_guid,
                                            event_code,
                                            device_SERVICE,
                                            device_NOTICE,
                                            sen1NAME,
                                            sen2NAME,
                                            arrive_user,
                                            accept_number,
                                            arrive_code,
                                            sync_time,
                                            arrive_time,
                                            batch_guid,
                                            arrive_desc,
                                            createddate,
                                            user_name
                                            )values(
                                            ?,
                                            ?,
                                            ?,
                                            ?,
                                            ?,
                                            ?,
                                            ?,
                                            ?,
                                            ?,
                                            Getdate(),
                                            ?,
                                            ?,
                                            ?,
                                            ?,
                                            ?
                                            )
                                                ''';
                            jt.update(InsertSql2,details_guid,EVENT_CODE,device_SERVICE,device_NOTICE,sen1NAME,sen2NAME,arrive_user,accept_number,arrive_code,arrive_time,bacth_guid,arrive_desc,createddate,user_name);



                        }


                    }else {
                        log.debug("【apiGetKJEventList】接口：返回BatchDescList批次详情数组为空！");

                    }


                }

                long EndTime = System.currentTimeMillis();
                outData.put("code","0");
                outData.put("msg","【apiGetKJEventList】接口：处理成功！耗时(毫秒)："+(EndTime-startTime));
                log.debug("【apiGetKJEventList】接口处理耗时(毫秒)："+(EndTime-startTime));
            }else{
                log.debug("【apiGetKJEventList】接口：返回data批次数组为空,不继续操作！");
                outData.put("code","-1");
                outData.put("msg","【apiGetKJEventList】接口：返回批次数组为空,不继续操作");

            }





        }else{
            outData.put("code",KHUtil.GetSFJV(jsonObject,"code"));
            outData.put("msg",KHUtil.GetSFJV(jsonObject,"msg"));
        }




    }catch (Exception e) {
            log.error("【apiGetKJEventList】接口异常:" + e.getMessage());
            outData.put("code", "-1");
            outData.put("msg", "【apiGetKJEventList】接口异常:" +e.getMessage());
        } finally {
            LogToDB.toDB(eliteBean, "dataSource", "自动获取客景系统事件数据API[普通]", "apiGetKJEventList", inData.toString(), outData.toString(), (System.currentTimeMillis() - startTime).toString(), "/");
        }



     log.debug("【apiGetKJEventList】处理完成！"+outData.toString());



    }
}
